package com.ymatou.iapi.couponservice.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponservice.parameter.UseCouponBean;
import com.ymatou.iapi.couponservice.parameter.UseCouponCallbackBean;
import com.ymatou.iapi.couponservice.parameter.UseCouponInfo;
import com.ymatou.iapi.couponservice.service.UseCouponCallbackCall;
import com.ymatou.iapi.couponservice.service.UserReceiveCouponCall;
import com.ymatou.iapi.m.couponservice.parameter.CreateCouponBean;
import com.ymatou.iapi.m.couponservice.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.m.couponservice.service.CreateCouponCall;
import com.ymatou.iapi.m.couponservice.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceCallService;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.TestDataManager;
/**
*优惠券使用回调
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_UseCouponCallback{
	private static UseCouponCallbackBean usecouponcallbackBean;
	private static UseCouponCallbackCall usecouponcallbackCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("优惠券使用回调");
	}

	@Before
	public void caseUp() {
		usecouponcallbackBean=new UseCouponCallbackBean();
		usecouponcallbackCall=new UseCouponCallbackCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(usecouponcallbackCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UseCouponCallback_001() {
		Logger.start(true,"优惠券使用回调,成功返回ok-平台领用券");
		try {
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponServiceMCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			CreateCouponCall createCouponCall= couponServiceMCallService.createPlatformCoupon(createCouponBean);
			
			UserReceiveCouponCall userReceiveCouponCall = couponServiceMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			String ymtCouponCode =userReceiveCouponCall.getUserReceiveResult().getCouponCode();
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			YmtCouponSetting setting1 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			int iMaxUseTime1 = setting1.getiMaxUseTime();
			CouponServiceCallService couponServiceCallService = new CouponServiceCallService();
			new YmtCouponWapper().useCoupon(userId, ymtCouponCode,orderId);
			
			usecouponcallbackBean = couponServiceCallService.setUseCouponCallbackBean(orderId, null, ymtCouponCode);
			
			usecouponcallbackCall.setData(usecouponcallbackBean);
			usecouponcallbackCall.callService();	
			
			Logger.verifyEquals("ok", usecouponcallbackCall.getReturnData(), "验证返回");
			
			YmtCouponSetting setting2 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			int iMaxUseTime2 = setting2.getiMaxUseTime();
			
			Logger.verifyEquals(1, iMaxUseTime1-iMaxUseTime2, "验证setting表iMaxUseTime");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_UseCouponCallback_002() {
		Logger.start(true,"优惠券使用回调,成功返回ok-商家券");
		try {
			int userId = TestDataManager.getRandomIntNum(8);
			int orderId = TestDataManager.getRandomIntNum(10);
			CouponServiceMCallService couponServiceMCallService = new CouponServiceMCallService();
			
			SellerCreateBatchBean sellerCreateBatchBean = couponServiceMCallService.setDefaultSellerCreateBatchBean();
			SellerCreateBatchCall createCouponCall= couponServiceMCallService.SellerCreateBatch(sellerCreateBatchBean);
		
			UserReceiveCouponCall userReceiveCouponCall = couponServiceMCallService.receiveCouponInfo(createCouponCall.getBatchCode(), userId);
			
			String ymtCouponCode =userReceiveCouponCall.getUserReceiveResult().getCouponCode();
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			YmtCouponSetting setting1 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			int iMaxUseTime1 = setting1.getiMaxUseTime();
			CouponServiceCallService couponServiceCallService = new CouponServiceCallService();
			new YmtCouponWapper().useCoupon(userId, ymtCouponCode,orderId);
			
			usecouponcallbackBean = couponServiceCallService.setUseCouponCallbackBean(orderId, null, ymtCouponCode);
			
			usecouponcallbackCall.setData(usecouponcallbackBean);
			usecouponcallbackCall.callService();	
			
			Logger.verifyEquals("ok", usecouponcallbackCall.getReturnData(), "验证返回");
			
			YmtCouponSetting setting2 = new YmtCouponSettingWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			int iMaxUseTime2 = setting2.getiMaxUseTime();
			
			Logger.verifyEquals(1, iMaxUseTime1-iMaxUseTime2, "验证setting表iMaxUseTime");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}